#!/usr/bin/env python2.7
# -*- coding:utf-8 -*- 

# china_9km_anl.py

import os, sys
from metlib.kits import *
from datakeeper.models import DataSet, DatasetMold

ds_name = 'china_9km_anl'
mold_name = 'WRF'
ds_desc = u'大贤的全国9km再分析数据集'


def run():
    try:
        raise DataSet.DoesNotExist
        ds = DataSet.objects.get(name=ds_name)
        print "%s exists, not creating." % ds_name
    except DataSet.DoesNotExist:
        try:
            mold = DatasetMold.objects.get(name=mold_name)
        except DatasetMold.DoesNotExist:
            print "No such dataset-mold: " + mold_name
            sys.exit(1)
        print "Creating dataset: %s ..." % ds_name
        ds = DataSet(
            name=ds_name,
            desc=ds_desc,
            creator='dxwind',
            version='1.0',
            mold=mold,
            priority=10,
            protect_data=True,
        )
        ds.info = {
            "name": "china_9km_anl",
            "zh_name": u"全国9km再分析(C9A)",
            "version": 1,
            "desc": ds_desc,
            "WRF_version": "",
            "begdt": "2010-01-01 00",
            "enddt": "2015-01-01 00",
            "years": ["2010", "2011", "2012", "2013", "2014"],
            "levels": ["100", "80", "70", "10"],
            "domain_info": {"1": {"available": True, "priority": 7, "trim": 0},
                            "2": {"available": True, "priority": 3, "trim": 4},
                            "3": {"available": True, "priority": 4, "trim": 4},
                            "4": {"available": True, "priority": 1, "trim": 2},
                            "5": {"available": True, "priority": 2, "trim": 4},
                            "6": {"available": True, "priority": 5, "trim": 4},
                            "7": {"available": True, "priority": 6, "trim": 4}},
            "wps_namelist": """
&share
 wrf_core   = 'ARW',
 max_dom = 7,
 start_date = '2013-01-04_12:00:00', '2013-01-04_12:00:00', '2013-01-04_12:00:00', '2013-01-04_12:00:00', '2013-01-04_12:00:00', '2013-01-04_12:00:00', '2013-01-04_12:00:00',
 end_date   = '2013-01-08_00:00:00', '2013-01-08_00:00:00', '2013-01-08_00:00:00', '2013-01-08_00:00:00', '2013-01-08_00:00:00', '2013-01-08_00:00:00', '2013-01-08_00:00:00',
 interval_seconds = 21600
 io_form_geogrid = 2,
/

&geogrid
 parent_id         =   1,   1,  1,  1, 1,1,1,
 parent_grid_ratio =   1,   3,  3,  3, 3,3,3,
 i_parent_start    =   1,   133,74, 15,76,140,155,
 j_parent_start    =   1,   5, 18, 76,60,60,117,
 e_we              =   241,  187, 187, 190,202,187,202,
 e_sn              =   187,  181, 136, 232,181,187,181,
 geog_data_res     =   'modis_30s+5m','modis_30s+2m','modis_30s+2m','modis_30s+2m','modis_30s+2m','modis_30s+2m','modis_30s+2m',
 dx = 27000,
 dy = 27000,
 map_proj = 'lambert',
 ref_lat   =  38,
 ref_lon   =  104,
 truelat1  =  5.0,
 truelat2  =  10.0,
 stand_lon =  104,
 geog_data_path = '/mnt/static_data/geog/WPS_GEOG',
/
            """
        }

        try:
            ds.save(update_fields=['desc', 'creator', 'version', 'mold', 'priority', 'protect_data', 'info_json'])
        except Exception:
            ds.save()